/*
 * Copyright (C) 2001-2005 Pleasant nightmare studio
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.pleasantnightmare.noraxidium.support;

import org.apache.log4j.Logger;
import org.pleasantnightmare.noraxidium.SupportContainer;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Manages retrieval, loading, cacheing and unloading of resources.
 *
 * @author deus
 * @version 1.0
 * @since Jul 6, 2010 2:43:45 PM
 */
public class ResourcesSupport implements Support {
  private static final Logger LOGGER = Logger.getLogger(ResourcesSupport.class);
  private SupportContainer supportContainer;

  private byte[] tempLoadData;
  private ByteArrayOutputStream baos;
  private Map<String, ByteBuffer> cache;

  public ResourcesSupport() {
    cache = new HashMap<String, ByteBuffer>();
    tempLoadData = new byte[1024];
    baos = new ByteArrayOutputStream();
  }

  @Override public void setSupportContainer(SupportContainer supportContainer) {
    this.supportContainer = supportContainer;
  }

  public ByteBuffer loadResourceBlob(String resourcePath, CachePolicy cachePolicy) {
    try {
      Arrays.fill(tempLoadData, (byte) 0);
      baos.reset();

      readAll(resourcePath);

      ByteBuffer datachunk = ByteBuffer.wrap(baos.toByteArray());
      LOGGER.debug("Loaded " + datachunk.limit() + " bytes of data for path '" + resourcePath);

      if (cachePolicy.equals(CachePolicy.CACHE_UNTIL_MAP_CHANGE)) {
        cache.put(resourcePath, datachunk);
        LOGGER.debug("Data for '" + resourcePath + "' cached.");
      }

      datachunk.rewind();
      return datachunk;
    } catch (IOException e) {
      throw new IllegalStateException(e);
    }
  }

  private void readAll(String resourcePath) throws IOException {
    if (resourcePath == null)
      throw new IllegalArgumentException("resourcePath == null");

    InputStream stream = ClassLoader.getSystemResourceAsStream(resourcePath);
    if (stream == null)
      throw new IOException("Couldn't find resource: " + resourcePath);

    int read = stream.read(tempLoadData);
    do {
      baos.write(tempLoadData, 0, read);
      read = stream.read(tempLoadData);
    } while (read != -1);

    stream.close();
  }

  public enum CachePolicy {
    DONT_CACHE,
    CACHE_UNTIL_MAP_CHANGE,
  }
}
